//
//  VisitorLoginView.swift
//  54_Weibo(swift)
//
//  Created by 梦然 张 on 16/4/7.
//  Copyright © 2016年 梦然 张. All rights reserved.
//

import UIKit

class VisitorLoginView: UIView {
    
// MARK: - 外部访问属性 & 重要变化属性
    
// MARK: - 外部访问方法
    
    func setupInfo(imageName: String?, message: String) {
    messageLabel.text = message
    guard let imgName = imageName else {
        startAnim()
        return
    }
    iconView.image = UIImage(named: imgName)
    homeIconView.hidden = true
    coverView.hidden = true
    }
// MARK: - 监听方法
    
// MARK: - 内部属性 & 懒加载控件
    
    
    
    /// 旋转图标
    private lazy var iconView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    /// 小房子
    private lazy var homeIconView = UIImageView(imageName: "visitordiscover_feed_image_house")
    /// 文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        label.numberOfLines = 0
        label.sizeToFit()
        return label
    }()
    /// 注册按钮
    private lazy var registerButton: UIButton = UIButton(title: "注册", color: UIColor.orangeColor(), fontSize: 14, backImgName: "common_button_white_disable")
    /// 登录按钮
    private lazy var loginButton: UIButton = UIButton(title: "登录", color: UIColor.grayColor(), fontSize: 14, backImgName: "common_button_white_disable")
    private lazy var coverView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    
// MARK: - 设置界面

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(white: 237.0/255, alpha: 1.0)
        setupUI()
    }

    /**
     当视图使用SB或Xib创建，调用此方法
     */
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 设置布局
    private func setupUI() {
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeIconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        // 取消 autoresizing
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        // 1> 图标
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -60))
        // 2> 小房子
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: homeIconView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        // 3> 文本框
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        // 4> 注册按钮
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .CenterX, multiplier: 1.0, constant: -10))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 8))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 4> 登录按钮
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .CenterX, multiplier: 1.0, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 8))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 36))
        // 5> 遮盖图片
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: .Height, relatedBy: .Equal, toItem: self, attribute: .Height, multiplier: 0.7, constant: 0))
    }
    private func startAnim() {
        let anim: CABasicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        anim.removedOnCompletion = false

        iconView.layer.addAnimation(anim, forKey: nil)
    }
//    private func startAnim() {
//        
//        let anim = CABasicAnimation(keyPath: "transform.rotation")
//        
//        anim.toValue = 2 * M_PI
//        anim.repeatCount = MAXFLOAT
//        anim.duration = 20
//        
//        // 用在不断重复的动画上，当动画绑定的图层对应的视图被销毁，动画会自动被销毁
//        anim.removedOnCompletion = false
//        
//        // 添加到图层
//        iconView.layer.addAnimation(anim, forKey: nil)
//    }
}
